<%@page import="tools.MyTool"%>
<%@page import="java.util.Iterator"%>
<%@page import="accessor.HomeProductAccessor"%>
<%@page import="accessor.Products"%>
<%@include file="no_banner_header.jsp" %>

<%

%>
<link href="../assets/css/frontend/order.css" rel="stylesheet" type="text/css" media="screen" />
<div>
    <script>
        $(document).ready(function() {
            $('#step1').addClass('current');
            $("a.cart_delete").click(function(){
                var productID = $(this).attr("href");
                var total = $("#cart_total").text();
                //alert(total);return false;
                var price = $("#price_id_"+productID).text();
                var self = $(this);
                $.ajax({
                    type: 'POST',
                    url: '../ajax_cart_del',
                    data: {
                        'productID' : productID
                    },
                    dataType:"json",
                    success: function(data)                          
                    {     
                        $('#cart_count').html(data);
                        $(self).parent().parent().hide(500);
                        $("#cart_total").html(total-price);
                    }
                });
                return false;
            });
        });
        function update_cart(update_value,productID,originalQty){
            var total_price = $("#cart_total").text();
            var deductedPrice = total_price - $("#price_id_"+productID).text();
            var price = $("#price_"+productID).text();
            var newTotal = deductedPrice+(update_value*price);
            $.ajax({
                type: 'POST',
                url: '../ajax_cart_update',
                data: {
                    'productID' : productID,
                    'quantity':update_value
                },
                dataType:"json",
                success: function(data)                          
                {     
                    if(data==1){
                        $("#price_id_"+productID).html(update_value*price);
                        $("#cart_total").html(newTotal);
                    } else{
                        alert("Only number are allowed here!");
                        $(".cart_quantity_input_"+productID).val(originalQty);
                    }
                }
            });
        }
    </script>
    <%
        List<Products> cartOrder = new ArrayList<Products>();
        Double totalPrice = 0.0;
        if (request.getSession().getAttribute("cart") != null) {
            cartOrder = (List<Products>) request.getSession().getAttribute("cart");
        } else {
            response.sendRedirect("index.jsp");
        }

    %>
    <div id="order_step">
        <h4>Steps of Order</h4>
        <p>
            <a id="step1" href="home_order.jsp">Products</a>
            <a id="step2" href="login_order.jsp">Login</a>
            <a id="step3" href="address_order.jsp">Address</a>
            <a id="step4" href="summary_order.jsp">Order</a>
        </p>
    </div>

    <form action="login_order.jsp" method="post" accept-charset="utf-8"><div class="break_line"></div>   
        <table id="order_table" cellspacing="1" cellpadding="6" border="0" style="width:100%">
            <tbody>
                <tr>
                    <th class="table_title_td" >Product Name</th>
                    <th  width="70">Image</th>
                    <th width="40">Qty</th>
                    <th width="80">Price</th>
                    <th width="80">Total</th>
                    <th width="40">Delete</th>
                </tr>
                <%for (int index = 0; index < cartOrder.size(); index++) {
                        Products item = cartOrder.get(index);
                        totalPrice += item.getPrice() * item.getQuantity();

                %>
                <tr>                  
                    <td class="table_title_td"><%=item.getProductName()%></td>        
                    <td><img src="../assets/img/p/<%=item.getDefaultImage()%>" width="64" height="48" style="border: 0px solid #DDDDDD;" />
                    </td>
                    <td>
                        <input type="text" name="qty" value="<%=item.getQuantity()%>" class="cart_quantity_input_<%=item.getProductID()%>" onkeyup="update_cart(this.value,'<%=item.getProductID()%>','<%=item.getQuantity()%>');" onblur="update_cart(this.value,'<%=item.getProductID()%>','<%=item.getQuantity()%>');" maxlength="3" size="5"  />
                    </td>
                    <td>$<span id="price_<%=item.getProductID()%>"><%=item.getPrice()%></span></td>
                    <td>$<span id="price_id_<%=item.getProductID()%>"><%=item.getPrice() * item.getQuantity()%></span></td>

                    <td><a href="<%=item.getProductID()%>" class="cart_delete">&nbsp;</a></td>
                </tr>       
                <%}%>
                <tr id="total_footer">
                    <td colspan="3"></td>
                    <td class="right"><strong>Total</strong></td>
                    <td>$<span id="cart_total"><%=totalPrice%></spsan></td>
                </tr>
                <tr id="empty_cart" style="display:none">
                    <td colspan="6" height="80">
                        There is no product in your cart!
                    </td>  
                </tr>

            </tbody>
        </table>
        <input type="button" id="shop_more" class="submit" value="Shop more" size="30" onclick="window.location.href='listing.jsp'" />        
        <input type="submit" class="submit" value="Next" name="submitSummaryNext" style="float:right;margin-right: 20px;"/>
    </form>
</div>                              

<div style="clear: both;">&nbsp;</div>
<%@include file="footer.jsp" %>